package com.uy.cuantoconocesdeuruguay.adapters;

import java.util.LinkedList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.uy.cuantoconocesdeuruguay.MainActivity;
import com.uy.cuantoconocesdeuruguay.R;
import com.uy.cuantoconocesdeuruguay.datatypes.FacebookUser;
import com.uy.cuantoconocesdeuruguay.dialogs.ErrorOccurredDialogUtils;
import com.uy.cuantoconocesdeuruguay.dialogs.FriendProfileDialogUtils;
import com.uy.cuantoconocesdeuruguay.exceptions.ConnectivityException;
import com.uy.cuantoconocesdeuruguay.server.ServerConnector;
import com.uy.cuantoconocesdeuruguay.utiles.ImageLoader;
import com.uy.cuantoconocesdeuruguay.utiles.StringPool;

public class FriendsViewAdapter extends ArrayAdapter<String> {
	private static Activity activity;
	private LinkedList<FacebookUser> friendsPlaying;

	public FriendsViewAdapter(Activity activity, String[] items, LinkedList<FacebookUser> friendsPlaying) {
		super(activity.getApplicationContext(), R.layout.activity_friends_ranking, items);
		FriendsViewAdapter.activity = activity;
		this.friendsPlaying = friendsPlaying;
	}

	@Override
	public View getView(int position, View friendView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (friendView == null) {
			LayoutInflater inflater = (LayoutInflater) activity.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			friendView = inflater.inflate(R.layout.view_friends, parent, false);
			viewHolder = new ViewHolder();
		    viewHolder.friendView = friendView;
		    viewHolder.challengeIcon = (ImageView) friendView.findViewById(R.id.challengeIcon);
		    viewHolder.friendPosition = (TextView) friendView.findViewById(R.id.friendPosition);
		    viewHolder.rankingPosition = (TextView) friendView.findViewById(R.id.friendPosition);
		    viewHolder.friendName = (TextView) friendView.findViewById(R.id.friendName);
		    viewHolder.friendScore = (TextView) friendView.findViewById(R.id.friendScore);
		    viewHolder.friendImage = (ImageView) friendView.findViewById(R.id.friendImage);
		    viewHolder.friendImageProgressBar = (ProgressBar)friendView.findViewById(R.id.friendImageProgressBar);
		    friendView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) friendView.getTag();
		}
		viewHolder.friendImageProgressBar.setVisibility(View.VISIBLE);
		viewHolder.friendImage.setImageResource(R.drawable.com_facebook_profile_default_icon);
	    
	    final FacebookUser friend = friendsPlaying.get(position);
	    viewHolder.rankingPosition.setText(String.valueOf(position+1));
	    if (friend.getId().equals(MainActivity.userData.getFacebookid())){
	    	viewHolder.challengeIcon.setVisibility(View.INVISIBLE);
	    	viewHolder.friendName.setText(friend.getFirst_name()+StringPool.SPACE+friend.getLast_name() + activity.getString(R.string.friends_view_adapter_me));
	    }else{
	    	viewHolder.friendName.setText(friend.getFirst_name()+StringPool.SPACE+friend.getLast_name());
		    viewHolder.challengeIcon.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					challengeFriend(friend.getFirst_name(), friend.getId(), null, activity);
				}
			});
	    }
	    ((TextView) friendView.findViewById(R.id.friendScore)).setText(String.valueOf(friend.getScore()));
	    	    
	    if ( !(friend.getPicture() == null || friend.getPicture().equals(StringPool.BLANK)) ){
	    	ImageLoader.loadImageOn(activity, viewHolder.friendImage, friend.getPicture(), viewHolder.friendImageProgressBar);
		}
		
	    friendView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				FriendProfileDialogUtils dialog = new FriendProfileDialogUtils(activity);
				dialog.createProfilePopup(friend);
			}
	    });
	    return friendView;
	}
	
	public static void challengeFriend(String friendName, final String friendId, final AlertDialog dialogCaller, final Activity activity){
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setTitle(activity.getString(R.string.friends_view_adapter_challenge_to) + friendName + StringPool.QUESTION);
	    builder.setPositiveButton(activity.getString(R.string.friends_view_adapter_challenge), new DialogInterface.OnClickListener() {
	    	@Override
	    	public void onClick(DialogInterface dialog, int id) {
	    		try {
					ServerConnector.challengeFriend(friendId);
				} catch (ConnectivityException e) {
					ErrorOccurredDialogUtils d = new ErrorOccurredDialogUtils(activity, e);
					d.createErrorOccurredPopup();
					e.printStackTrace();
					return;
				}
	    		Toast.makeText(activity, activity.getString(R.string.friends_view_adapter_challenge_sent), Toast.LENGTH_SHORT).show();
	        	if (dialogCaller != null){
	        		dialogCaller.dismiss();
	        	}
	    	}
	    });
	    builder.setNegativeButton(activity.getString(R.string.cancel), new DialogInterface.OnClickListener() {
	    	public void onClick(DialogInterface dialog, int id) {	    		
	    	}
	    });
	    builder.setCancelable(true);
	    AlertDialog dialog = builder.create();
	    dialog.show();
	}
	static class ViewHolder {
		protected View friendView;
		protected TextView friendPosition;
	    protected ImageView challengeIcon;
	    protected TextView rankingPosition;
	    protected TextView friendName;
	    protected TextView friendScore;
	    protected ImageView friendImage;
	    protected ProgressBar friendImageProgressBar;
	}
}